class Solution(object):
    def maxSumRangeQuery(self, nums, requests):
        n = len(nums)
        out = [0] * (n + 1)
        for e, v in requests:
            out[e] += 1
            out[v + 1] -= 1
        ans = [0] * n
        ans[0] = out[0]
        for i in range(1, n):
            ans[i] = ans[i - 1] + out[i]
        ans.sort()
        nums.sort()
        return sum([ans[i] * nums[i] for i in range(n)]) % (10 ** 9 + 7)


